<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/style.css"/>
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>


    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        let pageNum=null;
        let nextPage=null;
        let prePage=null;
        let pages=null;
        let isFirstPage=null;
        let isLastPage=null;
        $(function () {
            $('#newNav').click(function () {
                window.location.href = "/pages/resource/add.html";
            });
        });
        function checkall() {
            var alls = document.getElementsByName("check");
            var ch = document.getElementById("checkall");
            if ($(ch).prop("checked")) {
                for (var i = 0; i < alls.length; i++) {
                    $(alls[i]).prop("checked",true);
                }
            } else {
                for (var i = 0; i < alls.length; i++) {
                    $(alls[i]).prop("checked",false);
                }
            }
        }

        function delAll() {
            var alls = document.getElementsByName("check");
            var ids = new Array();
            for (var i = 0; i < alls.length; i++) {
                if ($(alls[i]).prop("checked")) {
                    ids.push($(alls[i]).val());
                }
            }
            console.log(ids);
            if (ids.length > 0) {
                if (confirm("是否删除?")){
                    for(var i=0;i<ids.length;i++){
                        console.log(ids[i]);
                        $.getJSON("/resource/deleteone.do",{mId:ids[i]},null)
                    }
                    alert("删除成功");
                    loadData(null);
                }
            } else {
                alert("请选中要删除的项");
            }
        }
let list2=new Array();
        function loadData(data) {
            let menuname = $("#menuname").val();
            $.getJSON("/resource/list.do", {menuname:menuname,pageNum:data}, function (vo) {
                let list = vo.pageInfo.list;
                let total=vo.pageInfo.total;
                pages=vo.pageInfo.pages;
                pageNum=vo.pageInfo.pageNum;
                prePage=vo.pageInfo.prePage;
                nextPage=vo.pageInfo.nextPage;
                isFirstPage=vo.pageInfo.isFirstPage;
                isLastPage=vo.pageInfo.isLastPage;
                let href="<a id=\"page"+1+"\" onclick=\"loadData(1)\">"+1+"</a>";
                for (let i=2;i<=pages;i++){
                    if (i==6){
                        break;
                    }
                    href+="<a id=\"page"+i+"\" onclick=\"loadData("+i+")\">"+i+"</a>"
                }
                $("#page").html(href);
                if(isFirstPage){//如果是第一页：上一页和首页禁用
                    $("#firstPage").hide();
                    $("#prePage").hide();
                }
                if(isLastPage){//如果已经是最后一页：下一页和末页禁用
                    $("#nextPage").hide();
                    $("#lastPage").hide();
                }
                if (!isFirstPage){
                    $("#firstPage").show();
                    $("#prePage").show();
                }
                if (!isLastPage){
                    $("#nextPage").show();
                    $("#lastPage").show();
                }
                let str = "";
                for (let i = 0; i < list.length; i++) {
                    let obj = list[i];
                    let mName = obj.mName;
                    let mUrl=obj.mUrl;
                    let mId = obj.mId;
                    list2.push(mId);
                    let mState = obj.mState;
                    let btnStr = "";
                    let state=null;
                    if (mState != 2) {
                        btnStr = '<button type="button" name="deleteBtn" onclick="deleteMenu(' + mId + ')">删除</button>&nbsp;&nbsp;' +
                            '<button type="button" name="updateBtn" onclick="updateMenu(' + mId + ')">编辑</button>';
                        if (mState==0){
                            state="有效";
                        }
                        if (mState==1){
                            state="无效";
                        }
                        if (mState==3){
                            state="未启用";
                        }
                    } else if (mState==2){
                        btnStr = '<button name="deleteBtn" class="btn btn-sm btn-danger disabled">已删除</button>';
                        state="已删除";
                    }
                    str += '<tr>' +
                        '<td style="vertical-align:middle;"><input type="checkbox" name="check" value='+mId+' onclick="notcheckall()"></td>' +
                        '            <td>' + mName + '</td>' +
                        '            <td>' + mUrl + '</td>' +
                        '            <td>' + state + '</td>' +
                        '            <td>' + btnStr + '</td>' +
                        // '                <a href="editUser.html">编辑</a>&nbsp;&nbsp;<a href="javascript:alert(\'删除成功！\');">删除</a>             ' +
                        '            </td>' +
                        '    </tr>'
                }
                $("#thead").html(str);
                $("#total").html(total);
                $("#pages").html(pages);
                $("#pageNum").html(pageNum);
            });
            let alls=document.getElementsByName("check");
            for(let i=0;i<alls.length;i++){
                $(alls[i]).attr("value",list2[i]);
            }
        }

        function deleteMenu(mId){
            if (confirm("是否删除?")){
                $.getJSON("/resource/deleteone.do",{mId:mId},function (vo){
                    if(vo.code==200){
                        alert(vo.msg);
                        loadData(null);
                    }else {
                        alert(vo.msg);
                    }
                });
            }
        }

        function updateMenu(id) {
            window.location.href="/pages/resource/edit.html?mId="+id;
        }

        $(function () {
            loadData(null);
        });


    </script>
</head>
<body>
<form id="myform" class="form-inline definewidth m20" action="index.html" method="get">
    资源(菜单)名称：
    <input type="text" name="menuname" id="menuname" class="abc input-default" placeholder="" value="">&nbsp;&nbsp;
    <button type="button" class="btn btn-primary" onclick="loadData()">查询</button>
</form>
<table class="table table-bordered table-hover definewidth m10">
    <thead>
    <tr>
        <th width="5%"><input type="checkbox" id="checkall" onclick="checkall()"></th>
        <th>资源名称</th>
        <th>路径Url</th>
        <th>是否有效</th>
        <th width="10%">操作</th>
    </tr>
    </thead>
    <thead id="thead">

    </thead>

</table>
<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <th colspan="5">
            <div class="inline pull-right page">
                <span id="total">-</span> 条记录 <span id="pageNum">-</span>/<span id="pages">-</span>页 <a id="firstPage" onclick="loadData(1)" >首页</a><a onclick="loadData(prePage)" id="prePage">上一页</a><span id="page"></span><a id="nextPage" onclick="loadData(nextPage)">下一页</a>
                <a id="lastPage" onclick="loadData(pages)">最后一页</a></div>
            <div>
                <button type="button" class="btn btn-success" id="newNav">添加资源</button>&nbsp;&nbsp;&nbsp;<button
                    type="button" class="btn btn-success" id="delPro" onClick="delAll();">删除选中
            </button>
            </div>
        </th>
    </tr>
</table>
</body>
</html>